import { BadRequestException, Controller, Get, Param, Query, Redirect } from '@nestjs/common';
import { UniqueCodeService } from './unique-code.service';

@Controller('unique-code')
export class UniqueCodeController {
  constructor(private readonly uniqueCodeService: UniqueCodeService) {}

  @Get('short-url')
  async generateShortUrl(@Query('url') longUrl) {
    return this.uniqueCodeService.generate(longUrl)
  }

  @Get('/:code')
  @Redirect()
  async jump(@Param('code') code) {
    const longUrl = await this.uniqueCodeService.getLongUrl(code)
    if (!longUrl) {
      throw new BadRequestException('短链不存在')
    }
    return {
      url: longUrl,
      statusCode: 302
    }

  }
}
